﻿using System;
using System.Linq;
using System.Windows.Forms;
using DataAccess;
using DevExpress.XtraEditors;
using NLog;

namespace QuanLyKhachSan
{
    public partial class FormChiTietHoaDonCapNhat : XtraForm
    {
        private readonly ChiTietHoaDonDVManager _ctManager = new ChiTietHoaDonDVManager();
        protected static Logger _logger = LogManager.GetCurrentClassLogger();

        public FormChiTietHoaDonCapNhat()
        {
            InitializeComponent();
        }

        public FormChiTietHoaDonCapNhat(ChiTietHoaDonDichVu chiTietHoa)
        {
            InitializeComponent();
            InitChiTietForm(chiTietHoa);
        }

        private void InitChiTietForm(ChiTietHoaDonDichVu chiTietHoa)
        {
            lEditMaDV.EditValue = chiTietHoa.MaDV;
            lEditMaHoaDon.EditValue = chiTietHoa.MaHoaDonDV;
            nbSoLuong.Value = chiTietHoa.SoLuong;
            if (!btnThaoTac.Text.Equals("Add"))
            {
                lEditMaDV.Properties.ReadOnly = false;
                lEditMaHoaDon.Properties.ReadOnly = false;
            }
            else
            {
                lEditMaDV.Properties.ReadOnly = true;
                lEditMaHoaDon.Properties.ReadOnly = true;
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            var chiTietHoa = GetData();
            if (chiTietHoa != null)
            {
                var validateRs = _ctManager.Validate(chiTietHoa);
                if (validateRs.Valid())
                {
                    var acResult = new ActionResult();
                    if (btnThaoTac.Text.Equals("Add"))
                    {
                        acResult = _ctManager.Add(chiTietHoa);
                    }
                    else
                    {
                        acResult = _ctManager.InsertOrUpdate(chiTietHoa);
                    }
                    if (!acResult.Sucess())
                    {
                        _logger.Error(acResult);
                        XtraMessageBox.Show(acResult.GetErrorMessage());
                    }
                    else
                    {
                        DialogResult = DialogResult.OK;
                    }
                }
                else
                {
                    _logger.Error(validateRs.Message);
                    XtraMessageBox.Show(validateRs.ToString(), "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        private ChiTietHoaDonDichVu GetData()
        {
            var ct = new ChiTietHoaDonDichVu();
            try
            {
                ct.MaDV = Convert.ToInt32(lEditMaDV.EditValue);
                ct.MaHoaDonDV = Convert.ToInt32(lEditMaHoaDon.EditValue);
                ct.SoLuong = Convert.ToInt32(nbSoLuong.Value);
                return ct;
            }
            catch (Exception)
            {
                XtraMessageBox.Show("Vui lòng nhập đầy đủ thông tin");
                return null;
            }
        }

        private void FormChiTietHoaDonCapNhat_Load(object sender, EventArgs e)
        {
            try
            {
                lEditMaDV.Properties.DataSource = new DichVuManager().GetAllServices().Result.ToList();
                lEditMaHoaDon.Properties.DataSource = new HoaDonDichVuManager().GetListOrderServices().Result.ToList();
            }
            catch (Exception ex)
            {
                _logger.Error(ex.Message);
            }
        }

        private void nbSoLuong_Scroll(object sender, ScrollEventArgs e)
        {
            nbSoLuong.Value += 1;
        }
       
    }
}
